package com.yantong.service.impl;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yantong.service.MyWxpayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: juzi
 * @date: 2021/11/17
 * @time: 11:56
 */
@Service
public class MyWxPayServiceImpl implements MyWxpayService {

    @Autowired
    private WxPayService wxPayService;

    @Override
    public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
        return wxPayService.createOrder(request);
}


    @Override
    public WxPayMpOrderResult createJSPayRequest(String openid, String desc, String payNo, Double price, String notifyUrl, String ip, String toJSONString) throws WxPayException {
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        WxPayConfig config = wxPayService.getConfig();
        request.setNotifyUrl(notifyUrl);
        request.setOpenid(openid);
        request.setBody(desc);
        request.setOutTradeNo(payNo);
        request.setTradeType("JSAPI");
        request.setTotalFee(BaseWxPayRequest.yuanToFen(price.toString()));
        //用户ip
        request.setSpbillCreateIp(ip);
        request.setAttach(toJSONString);
        request.checkAndSign(config);

        return wxPayService.createOrder(request);
    }

    @Override
    public WxPayRefundResult refund(String transactionId, String outTradeNo, String outRefundNo, Double totalFee, Double refundFee) throws WxPayException {
        WxPayRefundRequest request = new WxPayRefundRequest();
        request.setTransactionId(transactionId);
        request.setOutTradeNo(outTradeNo);
        request.setOutRefundNo(outRefundNo);
        request.setTotalFee(BaseWxPayRequest.yuanToFen(totalFee.toString()));
        request.setRefundFee(BaseWxPayRequest.yuanToFen(refundFee.toString()));
        return this.wxPayService.refund(request);
    }
}





